'''Debug functions '''

import location



class LocationDebug(object):
    '''Class designed to be used by locations specifically for the purpose of adding print behavior'''
    
    @staticmethod
    def visit_locations(loc, fn):
        '''A debug function used to print out information about the gameboard'''
        queue = [(0,loc)]
        visited = set([loc])

        while queue:
            (depth, location) = queue.pop()
            new_locations = fn(location, depth)

            queue = [(depth+1, loc) for loc in (new_locations - visited)] + queue
            visited.update(new_locations)

    def print_all(self, depth):
        return set()

class StartingSquareDebug(object):
    def print_all(self, depth):
        occupants = ','.join([repr(occ) for occ in self.occupants])
        print '%s%s %s: %s' % ('\t' * depth, self.__class__.__name__, repr(self),
                               repr(occupants) if occupants else '<no occupants>')
        return set()


class RoomDebug(object):
    def print_all(self, depth):
        occupants = (repr(occ) for occ in self._query_occupants(lambda x: type(x) is PlayerPiece))
        weapons   = (repr(w) for w in self._query_occupants(lambda x: type(x) is WeaponPiece))
        tab_level = '\t' * depth
        print '%s%s: %s: %s %s' % (tab_level, self.__class__.__name__, self.name,
                                   '[%s]'%(','.join(occupants)) if occupants else '<no occupants>',
                                   '[%s]'%(','.join(weapons)) if weapons else '<no weapons>')

        return set([e for e in self.entrances.itervalues() if e])

class HallwayDebug(object):
    def print_all(self, depth):
        tab_level = '\t' * depth

        occupants = [repr(occ) for occ in self.occupants]
        print'%s%s: %s: %s' % (tab_level, self.__class__.__name__, repr(self),
                               '[%s]'%(','.join(occupants)) if occupants else '<no occupants>')
        rooms = list(self._get_rooms())
        ss = self.starting_square
        locations = rooms + [ss] if ss else rooms
        
        return set(locations)
    


def print_locations(loc):
    ''' Debug function to print out current state of game board'''

    def anon(l,d):
        return l.print_all(d)
    
    LocationDebug.visit_locations(loc, anon)


if __name__ == '__main__':
    pass
